package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "频道管理", tags = "channel", description = "频道管理API")
@RequestMapping("api/v1/channel")
public class AdChannelController {

    @Autowired
    AdChannelService adChannelService;

    @PostMapping("list")
    @ApiOperation("频道分页列表查询")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto){
        return  adChannelService.findByNameAndPage(dto);
    }

    @PostMapping("save")
    @ApiOperation("频道新增")
    public ResponseResult insert(@RequestBody AdChannel adchannel){
        return adChannelService.insert(adchannel);
    }

    @PostMapping("update")
    @ApiOperation("频道修改")
    public ResponseResult update(@RequestBody AdChannel adChannel){
        return adChannelService.update(adChannel);
    }

    @GetMapping("del/{id}")
    @ApiOperation("频道删除")
    public ResponseResult deleteById(@PathVariable Integer id){

//        if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        return adChannelService.deleteById(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = adChannelService.list();
        ResponseResult responseResult = ResponseResult.okResult(list);
        responseResult.setErrorMessage("现在我想用jenkins自动构建任务");
        return responseResult;
    }

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id){
    return ResponseResult.okResult(adChannelService.getById(id));
    }
}

